package com.example.hms.service.servicelmpl;

import com.example.hms.dao.LiveDao;
import com.example.hms.entity.Live;
import com.example.hms.entity.Reserve;
import com.example.hms.entity.SystemLog;
import com.example.hms.service.LiveService;
import com.example.hms.service.SystemLogService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class LiveServiceImpl implements LiveService {
    @Resource
    LiveDao liveDao;
    @Resource
    SystemLogService systemLogService;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // 查询所有入住信息
    @Override
    public Result selectAllLive(Live live) {
        return ResultSubmit.success(liveDao.selectAllLive(live));
    }


    // 结算入住信息
    @Override
    public Result settlementLive(Live live, HttpServletRequest request) {
        // 从数据库中获取Live对象的详细信息
        live = liveDao.selectAllLive(live).get(0);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // 解析入住时间和退房时间
            Date liveTime = df.parse(live.getLiveTime());//入住时间
            Date cancelTime=df.parse(live.getCancelTime());//退房时间
            // 计算时间差，并转换为天数
            Long num= cancelTime.getTime()-liveTime.getTime();//时间戳相差的毫秒数
            BigDecimal day = BigDecimal.valueOf(num/24/60/60/1000).add(BigDecimal.ONE);//除以一天的毫秒数计算天数
            // 根据天数和房间价格计算总金额
            live.setLiveAmount(day.multiply(live.getRoomPrice()));//并计算金额
        } catch (ParseException e) {e.printStackTrace();}
        // 更新和插入Live对象到数据库
        int i = liveDao.update(live);
        int j = liveDao.insert(live);

        String liveId = String.valueOf(live.getLiveTableId());
        live.setLiveTableId(null);
        live.setAbrogateIdentity(null);
        live.setState(null);
        Live live1 = liveDao.selectAllLive(live).get(0);

        // 系统记录
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("live");

        systemLog.setUseTableDataIdBefore(String.valueOf(liveId));
        systemLog.setUseTableDataIdAfter(String.valueOf(live1.getLiveTableId()));

        systemLog.setUseType("用户结算");
        systemLogService.insert(systemLog);

        // 根据数据库操作结果返回相应的结果
        if(i == 1 && j == 1){
            return ResultSubmit.success("成功");
        }else {
            // 如果操作失败，设置事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }
    }

    // 收款
    @Override
    public Result collectionLive(Live live, HttpServletRequest request) {
        live = liveDao.selectAllLive(live).get(0);
        int i = liveDao.update(live);
        live.setCollectionIdentity("已收款");
        int j = liveDao.insert(live);

        String liveId = String.valueOf(live.getLiveTableId());
        live.setLiveTableId(null);
        live.setAbrogateIdentity(null);
        live.setState(null);
        Live live1 = liveDao.selectAllLive(live).get(0);

        // 系统记录
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("live");

        systemLog.setUseTableDataIdAfter(String.valueOf(liveId));
        systemLog.setUseTableDataIdBefore(String.valueOf(live1.getLiveTableId()));

        systemLog.setUseType("用户确认收款");
        systemLogService.insert(systemLog);

        if(i == 1 && j == 1){
            return ResultSubmit.success("成功");
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }
    }


}
